package com.haian.slidealbum.control;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.haian.slidealbum.R;
import com.haian.slidealbum.model.helper.DBHandler;

public class WidgetSettingsActivity extends Activity implements OnClickListener {

	LinearLayout ic_back, lnSlideShowInterval;
	TextView txtSlideShowInterval;
	Dialog dialog;
	int intervalCurent;
	Context mContext;
	int interval = 5;
	
	// _______________________ System Fuction ________________________________

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_widget_setting);
		mContext = this;
		initWidget();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lnSlideShowInterval:
			showDialogInterval();
			break;
		case R.id.ic_back:
			finish();
			break;
		default:
			break;
		}

	}

	// _______________________________- Custom Function ________________________

	private void initWidget() {
		try {
			ic_back = (LinearLayout)findViewById(R.id.ic_back);
			ic_back.setOnClickListener(this);

			lnSlideShowInterval = (LinearLayout) findViewById(R.id.lnSlideShowInterval);
			lnSlideShowInterval.setOnClickListener(this);
			txtSlideShowInterval = (TextView) findViewById(R.id.txtSlideShowInterval);
			setTextInterval(String.valueOf(DBHandler.getInterval(mContext)));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void setTextInterval(String strInterval) {
		txtSlideShowInterval.setText(strInterval + " " + getResources().getString(R.string.seconds));
	}

	private void showDialogInterval() {
		try {
			dialog = new Dialog(this);
			dialog.setTitle(getResources().getString(R.string.slideshow_interval));
			dialog.setContentView(R.layout.dialog_interval);
			final RadioGroup dialogInterval_edTime = (RadioGroup) dialog
					.findViewById(R.id.dialogInterval_edTime);
			dialogInterval_edTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
			{
			    public void onCheckedChanged(RadioGroup cbGroup, int checkedId)
			    {
			        RadioButton cbSelected = (RadioButton)cbGroup.findViewById(checkedId);
			        if(cbSelected.isChecked())
			        {
			        	String valueStr = cbSelected.getText().toString();
			        	if(cbSelected.isChecked())
				        {
				        	if(valueStr.equals(mContext.getResources().getString(R.string._5_seconds)))
				        	{
				        		interval = 5;
				        	}else if(valueStr.equals(mContext.getResources().getString(R.string._10_seconds)))
				        	{
				        		interval = 10;
				        	}else if(valueStr.equals(mContext.getResources().getString(R.string._15_seconds)))
				        	{
				        		interval = 15;
				        	}else if(valueStr.equals(mContext.getResources().getString(R.string._20_seconds)))
				        	{
				        		interval = 20;
				        	}else if(valueStr.equals(mContext.getResources().getString(R.string._25_seconds)))
				        	{
				        		interval = 25;
				        	}else if(valueStr.equals(mContext.getResources().getString(R.string._30_seconds)))
				        	{
				        		interval = 30;
				        	}
				        }
			        }
			    }
			});
			
			Button dialog_BtnYes = (Button) dialog
					.findViewById(R.id.dialog_BtnYes);
			Button dialog_BtnNo = (Button) dialog
					.findViewById(R.id.dialog_BtnNo);
			dialog_BtnYes.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setTextInterval(String.valueOf(interval));
					DBHandler.updateInterval(mContext, String.valueOf(interval));
					WidgetUpdateService.updateInterval = true;
					dialog.dismiss();
				}
			});
			dialog_BtnNo.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.show();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
